(function () {
  var E = window.wangEditor;
  var editor = new E("#editor");
  // 或者 var editor = new E( document.getElementById('editor') )
  editor.create();

  layui.use(["table", "element", "form", "upload"], function () {
    var table = layui.table;
    var element = layui.element;
    var form = layui.form;
    var $ = layui.jquery;
    var upload = layui.upload;
    var url = GlobalSiteUrl + "/manage/postNews";

    //普通图片上传
    var uploadInst = upload.render({
      elem: "#uploadImg",
      url: url, //改成您自己的上传接口
      auto: false,
      bindAction: "#submit",
      acceptMime: "image/*",
      before: function (obj) {
        var file = document.querySelector("input[type=file]").files.length;
        //预读本地文件示例，不支持ie8
        obj.preview(function (index, file, result) {
          $("#img").attr("src", result); //图片链接（base64）
        });
        var FromData = $(".addForm").serializeArray();
        let title = FromData[0].value;
        let text = editor.txt.html();
        let suid = localStorage.getItem("suid");
        this.data = { suid, title, text };
      },
      done: function (res) {
        layer.msg(res.msg);
        setTimeout(function () {
          location.reload();
        }, 1000);
      },
      error: function () {
        //演示失败状态，并实现重传
        var demoText = $("#imgName");
        demoText.html(
          '<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>'
        );
        demoText.find(".demo-reload").on("click", function () {
          uploadInst.upload();
        });
      },
    });

    $(".post").on("click", function (res) {
      var file = document.querySelector("input[type=file]").files.length;
      var FromData = $(".addForm").serializeArray();
      const title = FromData[0].value;
      let text = editor.txt.html();
      if (title === "") {
        layer.msg("标题不能为空", {
          icon: 2,
          time: 1500,
        });
        return;
      } else if (text === "<p><br></p>") {
        layer.msg("请输入正文", {
          icon: 2,
          time: 1500,
        });
        return;
      } else if (file === 0) {
        layer.msg("请添加封面", {
          icon: 2,
          time: 1500,
        });
        return;
      }
    });
  });
})();
